;(function($){
	$.fn.megamenu = function(op){
		var timeout = null;
		var current = null;
		var $m = this;
		var ops = $.extend({},defaults, op),
		c_index = ops.index,
		over = function(){
			var item = $(this);
			if(timeout){
				clearTimeout( timeout );
			}
			c_index++;
			item.z_index = c_index;
			item.op = ops;
			// need to hidden sliblings items
			item.add(item.parents('div.col', $m)).add().siblings().each(function(){
				s_item = $(this);
				s_item.op = ops;
				s_item.hidemega();
			});
			item.showmega();
		},
		out = function(){
			var item = $(this);
			
			item.op = ops;
			
			clearTimeout(timeout);
			timeout = setTimeout(function(){
				item.hidemega();
			}, ops.delay);
		},
		hide_siblings = function(item){
			item.siblings().each(function(){
				
			});
		}
		return this.each(function() {			
			$(ops.hidden, this).not(ops.ignore).find(' > ' + ops.child).each(function(){
				$(this).css({
					'position': 'absolute',
				  	'left': '-999em'
				});
			});
			
			$(ops.hidden, this).not(ops.ignore).find(' > ' + ops.child).each(function(){
				this.w = $(this).outerWidth();
				this.h = $(this).outerHeight();
			});
			
			$(ops.hidden + ':has(' + ops.child + ')', this).not(ops.ignore)['hover'](over,out);
			
		});
	};

	var defaults = {
		hoverClass	: 'over',
		direction	: 'down',
		hidden		: 'li',
		child		: '.mega',
		menu		: '.mega-menu',
		ignore		: '.show-child',
		delay		: 1000,
		animateOver : 'jswing',
		animateOut : 'jswing',
		duration 	: 200,
		index		: 100
	};
	
	$.fn.extend({
		hidemega : function(){
			var op = this.op;
			var dir = op.direction;
			
			var items = $(this).find(op.child);
			if(items.length < 1) return this;
			
			$(items).each(function(){
				if($(this).parent().not(op.ignore).length >= 1){
					
					if(!$(this).hasClass('level1')){
						dir = 'left';
					}
					
					if(dir == 'down'){
						var h = $(this).outerHeight();
						$(this).stop().animate({
							height:'0px'
						}, op.duration, op.animateOut, function(){
							$(this).css('left', '-999em');
							$(this).css('height', h + 'px');
							$(this).removeClass(op.hoverClass);
						});
					} else {
						// backup width of item
						var w = $(this).outerWidth();
						$(this).stop().animate({
							width:'0px'
						}, op.duration , op.animateOut, function(){
							$(this).css('left', '-999em');
							$(this).css('width', w + 'px');
							$(this).removeClass(op.hoverClass);
						});
					}
				}
			});
			
			
			return this;
		},
		
		showmega : function(){
			var op = this.op;
			var dir = op.direction;
			
			var items = $(this).find(' > ' + op.child);
			
			if(items.length < 1) return this;
			
			if(!$(items[0]).hasClass('level1')){
				dir = 'left';
			}
			if(dir == 'down'){
				var item = $(items[0]);
				
				var h = item.outerHeight();
				if(items[0].h == undefined){
					items[0].h = h;
				}else {
					h = items[0].h;
				}
				var left = item.css('left');
				
				var height = item.outerHeight();
				
				item.addClass(op.hoverClass);
				if(left != 'auto'){
					item.css('left', 'auto');
					item.css('z-index', this.z_index);
					item.css('height', '0px');
					item.stop().animate({
						height:h + 'px'
					}, op.duration, op.animateOver, function(){
						$(this).css('height', h + 'px');
						$(this).css('overflow', 'visible');
					});
				}
			}else {
				var item = $(items[0]);
				var w = item.outerWidth();
				
				if(items[0].w == undefined){
					items[0].w = w;
				}else {
					w = items[0].w;
				}
				
				var left = item.css('left');
				var width = item.outerWidth();
				
				if(left != 'auto'){
					item.addClass(op.hoverClass);
					item.css('left', 'auto');
					item.css('z-index', this.z_index);
					
					item.css('width', '0px');
					
					item.stop().animate({
						width:w + 'px'
					}, op.duration, op.animateOver, function(){
						$(this).css('width', w + 'px');
						$(this).css('overflow', 'visible');
					});
				}
			}
			
			return this;
		}
	});
})(jQuery);